# testApp2.py
# AUTHOR: LEWIS MARHIN
# PURPOSE: To test and prototype GUI for CP304x Student Day Planner project.
# CONTROLS: RIGHT SOFT KEY : Exit

# Some code based off Nokia's ball.py program (standard with S60 python
# intstallation)

import e32
import appuifw
import cursor
from appuifw import *
from graphics import *
from key_codes import *

#app_lock = e32.Ao_lock()

# VARS used in drawing a dot on screen
size = 30
locX = 100
locY = 100

# Used to draw numbers down the side (for demonstration purposes)
hours = ["7","8","9","10","11","12","13","14","15","16","17","18","19","20","21"]

img=Image.new((300,500)) # Possibly is width x height. Should change to
                         # getWidth() and getHeight() which doesn't exist
                         # in python s60, probably something similiar does
                         # though.

def display(rect):
    canvas.blit(img)

# This var and function used to exit program
run = 1
def quit():
    global run
    run = 0
    #app_lock.signal()
appuifw.app.exit_key_handler = quit

class Keyboard(object):
    def __init__(self,onevent=lambda:None):
        self._keyboard_state={}
        self._downs={}
        self._onevent=onevent
    def handle_event(self,event):
        if event['type'] == appuifw.EEventKeyDown:
            code=event['scancode']
            if not self.is_down(code):
                self._downs[code]=self._downs.get(code,0)+1
            self._keyboard_state[code]=1
        elif event['type'] == appuifw.EEventKeyUp:
            self._keyboard_state[event['scancode']]=0
        self._onevent()
    def is_down(self,scancode):
        return self._keyboard_state.get(scancode,0)
    def pressed(self,scancode):
        if self._downs.get(scancode,0):
            self._downs[scancode]-=1
            return True
        else:
            return False

keyboard=Keyboard()

canvas = appuifw.Canvas(event_callback=keyboard.handle_event, redraw_callback = display) # Create new canvas
appuifw.app.body = canvas # Sets the screen to show the canvas object
appuifw.app.screen = 'full' # Fullscreen mode

# This function works like in OpenGL, it will loop
while run:
    img.clear(0xFFFFFF) # clears canvas for redraw (probably)
    #img.point((locX + size /2, locY + size /2), 0x000000, width = size)#draw dot

    ##### LINES #####
    # img.line([(x origin, y origin), (x dest., y dest.)], colour, options)
    # (See API 6.4.3 I think it is)
    # Change to black lines for demonstration
    img.line([(50, 50), (50, 400)], 0xFF0000, width = 3) # draw red line
    img.line([(0, 25), (350, 25)], 0x00FF00, width = 3) # draw blue line
    img.line([(0, 50), (350, 50)], 0x0000FF, width = 3) # draw green line
    ##### END LINES #####

    ##### SELECT CURSOR #####
    if keyboard.is_down(EScancodeDownArrow):
        startOffset += 5
    if keyboard.is_down(EScancodeUpArrow):
        startOffset -= 5
        
    cursor.drawCursor(img)
    ##### END SELECT CURSOR #####

    ###### TEXT #####
    # 1. These draw text, see section 6.4.4 of the API for font info
    # 2. NOTE SEE LINE 1 OF THIS WHILE LOOP, MULTIPLE FONTS DON'T WORK PROPERLY WITH img.clear() working
    #    WILL TRY AND FIX
    # 3. Will change CURRENT_DATE to the actual current date later
    img.text([(5, 20)], u"Student Day Planner", font = 'normal') # style
    img.text([(5, 45)], u"CURRENT_DATE", font = (u'LatinBold12', 15, appuifw.STYLE_BOLD)) # font
    # Line below, when uncommented, prevents either line from being rendered
    #img.text([(5, 45)], u"CURRENT_DATE", font = u'LatinBold12') # font

    # 1.    Goes through list of hours and prints them with a 0 up front if
    #       it is before 10
    # 2.    Font style is set to 'dense' which is small and squished (normal text)
    tmp = 0
    for hour in hours:
        if int(hour) <= 9:
            img.text([1, 72 + tmp], u" " + unicode(hour) + u":00", font = 'dense')
        else:
            img.text([1, 72 + tmp], unicode(hour) + u":00", font = 'dense')
        tmp+= 20
    ##### END TEXT #####

    e32.ao_yield() # THIS PREVENTS PROGRAM FROM FREEZING.
    display(())
    
#app_lock.wait()
